function PlotHitRates(ax,schedule)
%%
if ~exist('ax','var') || isempty(ax), ax = gca; end

% wp = schedule.writeparams;
% rp = schedule.readparams;
rv = schedule.response_vals(1:end-1,:);
rc = rv(:,2);

clear data

data.Quiet.bit = 11; data.Quiet.plotcode = 'ob';
data.Tone.bit  = 12; data.Tone.plotcode  = 'sg';
data.AM.bit    = 13; data.AM.plotcode    = '*r';    
    
fn = fieldnames(data);
k = 1;
lstr = [];
cla(ax)
hold on
for i = 1:length(fn)
    b = data.(fn{i}).bit;
    data.(fn{i}).ind = bitget(rc,b);
    n = sum(data.(fn{i}).ind);
    data.(fn{i}).avoids = bitget(rc,1)&data.(fn{i}).ind;
    data.(fn{i}).avoidrate = sum(data.(fn{i}).avoids)/n*100;
    y = cumsum(data.(fn{i}).avoids)./cumsum(data.(fn{i}).ind);
    data.(fn{i}).runningavoidrate = y(find(data.(fn{i}).ind)); %#ok<FNDSB>
    x = find(data.(fn{i}).ind);
    plot(ax,x,data.(fn{i}).runningavoidrate,data.(fn{i}).plotcode,'markersize',10);
    if ~isempty(x), lstr{k} = sprintf('%s (%0.1f%%)',fn{i},data.(fn{i}).avoidrate); k = k + 1; end %#ok<AGROW,SAGROW>
end
hold off
if ~isempty(lstr)
    h = legend(ax,lstr);
    set(h,'box','on','location','Best');
end
ylim([0 1]);
grid on
box on
title('Running Hit Rate');
xlabel('Trial number');
ylabel('Hit rate');
